package com.test.mooan.labama.activity_fragment_boom.ln_boom;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.test.mooan.labama.R;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseLnActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.login_boom.MobileVerificationActivity;
import com.test.mooan.labama.activity_fragment_boom.login_boom.MobileVerificationActivity2;
import com.test.mooan.labama.bean_boom.config_info_boom.AppShowInfo_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.CountryInfo_boom;
import com.test.mooan.labama.bean_boom.process_boom.ProcessInfo_boom;
import com.test.mooan.labama.bean_boom.product.Product;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.dialog_boom.DeleteDataDialog_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;
import com.test.mooan.labama.view_boom.widget_boom.TitleView_boom;

public class StatusRejectedActivity extends BaseLnActivity_boom {

    private View deleteDataBtn;

    private Product product;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_status_rejected);

        pageTag = "REJECT";

        try {
            product = (Product) getIntent().getSerializableExtra("product");
            if (product==null) product = Product.gtInstance(null);
        }catch (Exception e){
            if (Constant_boom.Debug) e.printStackTrace();
        }

        TitleView_boom titleView = findViewById(R.id.activity_rejected_view_title);
        setBackBtn_boom(titleView.getBackIb());

        titleView.setTitle(product.gtName());

        TextView textTv = findViewById(R.id.activity_rejected_tv_text);
        Button refreshBtn = findViewById(R.id.activity_rejected_btn_refresh_status);
        deleteDataBtn = findViewById(R.id.activity_rejected_btn_delete_data);

        String days = ProcessInfo_boom.getInstance().gDeniedBetweenDay();
        if (TextUtils.isEmpty(days)) days = AppShowInfo_boom.getInstance().gRejectPeriod();
        textTv.setText(getString(R.string.loan_fail_text).replaceAll("xxx",days));

        refreshBtn.setOnClickListener(v -> {
            UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_REFRESH_STATUS_CLICK",product.gtProduct());
            HttpManager_boom.getInstance().requestUserProcess(product.gtProduct(),product.gtToken());
        });

        onUserProcessResponse();

    }


    @Override
    public void onUserProcessResponse() {

        //手机号后置校验
        if (ProcessInfo_boom.getInstance().isNeedVerification()){
            if (CountryInfo_boom.gInstance().isCambodia()) startActivity_boom(MobileVerificationActivity.class);
            else startActivity_boom(MobileVerificationActivity2.class);
            return;
        }

        switch (ProcessInfo_boom.getInstance().gLoanStatus()){
            case Loan_Reviewing -> {
                Intent intent = new Intent(this, StatusReviewActivity.class);
                intent.putExtra("product",product);
                startActivity(intent);
                finish();
            }
            case Order_Pending -> {
                Intent intent = new Intent(this, StatusPendingActivity.class);
                intent.putExtra("product",product);
                startActivity(intent);
                finish();
            }
            case Loan_Rejected -> HttpManager_boom.getInstance().fetchDropConfig();
            case Order_Pending_Repayment, Order_Overdue_Repayment -> {
                LnMainActivity_boom.toRepayListFlag = true;
                onBackPressed();
            }
            default -> onBackPressed();
        }
    }

    public void onDropConfigResponse(boolean showDropButton){
        if (isClose()) return;

        if (showDropButton){
            deleteDataBtn.setVisibility(View.VISIBLE);
            deleteDataBtn.setOnClickListener(v -> {
                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_DELETE_DATA_CLICK").toUpperCase(),product.gtProduct());
                new DeleteDataDialog_boom(this).show();
            });
        }else deleteDataBtn.setVisibility(View.GONE);

    }

}